import Konva from 'konva';
export const handleStickImg = (layer,lastOperation)=>{
    if(layer.children) {
        layer.children.forEach(node=>{
            if(node.attrs.name==lastOperation.attrs.name && node.attrs.x ==lastOperation.attrs.x){
                node.fillPatternImage(null);
                layer.draw();
            }
        });
    }
   
}
export const handleAddImg = (layer,lastOperation)=>{
    if(layer.children) {
        layer.children.forEach(node=>{
            if(node.attrs.id==lastOperation.attrs.id && node.attrs.x ==lastOperation.attrs.x ){
                node.destroy()
                layer.draw();
            }
        });
    }
}
export const handleAddBackground = (layer,lastOperation)=>{
    console.log("1111");
    if(layer.children) {
        layer.children.forEach(node=>{
            if(node.attrs.id==lastOperation.attrs.id ){
                console.log(node);
                
                node.destroy()
                layer.draw();
            }
        });
    }
}
export const handleDrag = (operations,layer,lastOperation)=>{
    const previousState = operations
    .slice()
    .reverse()
    .find((op) =>op.attrs.id==lastOperation.attrs.id );    
  if (previousState) {
    const element = layer.findOne(`#${previousState.attrs.id}`);
    if (element) {
      element.position({
        x: previousState.attrs.x,
        y: previousState.attrs.y,
      });
      element.rotation(previousState.attrs.rotation);
      element.scaleX(previousState.attrs.scaleX);
      element.scaleY(previousState.attrs.scaleY);
    }
  }
}

export const handleDrawLine =(operations,layer,lastOperation)=>{
    if(layer.children) {
        layer.children.forEach(node=>{
            if(node.attrs.id==lastOperation.attrs.id && node.attrs.x ==lastOperation.attrs.x ){
                node.destroy()
                layer.draw();
            }
        });
    }
}
export const handleCreate = (operations,layer,lastOperation)=>{
    if(layer.children) {
        layer.children.forEach(node=>{
            console.log(node);
            
            if(node.attrs.id==lastOperation.attrs.id && node.attrs.x ==lastOperation.attrs.x ){
                node.destroy()
                layer.draw();
            }
        });
    }
}
export const  createRect =(id, x, y, stroke, fill, strokeWidth, height, width, name, draggable, rotation, scaleX, scaleY, skewX, skewY) =>{
    return new Konva.Rect({
      id: id,
      x: x,
      y: y,
      width: width,
      height: height,
      fill: fill,
      stroke: stroke,
      strokeWidth: strokeWidth,
      name: name,
      draggable: draggable,
      rotation: rotation, scaleX: scaleX, scaleY: scaleY, skewX: skewX, skewY: skewY
    });

  }
  export const createCircle =(id, x, y, stroke, fill, strokeWidth, radius, name, draggable, rotation, scaleX, scaleY, skewX, skewY)=> {
    return new Konva.Circle({
      id: id,
      x: x,
      y: y,
      radius: radius,
      fill: fill,
      stroke: stroke,
      strokeWidth: strokeWidth,
      name: name,
      draggable: draggable,
      rotation: rotation, scaleX: scaleX, scaleY: scaleY, skewX: skewX, skewY: skewY
    });
  }
  export const createTrapezoid= (id, x, y, stroke, strokeWidth, points, name, draggable, rotation, scaleX, scaleY, skewX, skewY )=>{
    return  new Konva.Line({
      id,
      x: x,
      y: y,
      closed:false,
      points:points,
      stroke: stroke,
      strokeWidth: strokeWidth,
      name: name,
      draggable: draggable,
      rotation: rotation, scaleX: scaleX, scaleY: scaleY, skewX: skewX, skewY: skewY
      });
  }